package com.ruoyi.qywx.getway.api;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.core.cache.Meta;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StrUtils;
import com.ruoyi.qywx.getway.entity.QyContactWay;
import com.ruoyi.qywx.getway.request.TxRequest;
import com.ruoyi.qywx.getway.request.TxResponse;
import com.ruoyi.qywx.model.vo.ContactVo;
import com.ruoyi.qywx.model.vo.ContactWayInfo;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author : hcl
 * @Date : 2023/8/11 10:18
 * https://developer.work.weixin.qq.com/document/path/95724
 */
public class ContactWayApi {
    /**
     * https://developer.work.weixin.qq.com/document/path/92576
     */
    public static TxResponse followUserList(Meta meta) {
        return TxRequest.getInstance(meta.getCorpid(), meta.getAppSecret())
                .get("/cgi-bin/externalcontact/get_follow_user_list");
    }

    public static TxResponse addContactWay(ContactWayInfo contactWay, Meta meta) {
        return TxRequest.getInstance(meta.getCorpid(), meta.getAppSecret())
                .post("/cgi-bin/externalcontact/add_contact_way", JSONUtil.toJsonStr(turnQyContactWay(contactWay)));
    }

    public static TxResponse updateContactWay(ContactWayInfo contactWay, Meta meta) {
        QyContactWay qyContactWay = turnQyContactWay(contactWay);
        JSONObject json = JSONUtil.parseObj(qyContactWay);
        json.remove("state");
        return TxRequest.getInstance(meta.getCorpid(), meta.getAppSecret())
                .post("/cgi-bin/externalcontact/update_contact_way", json.toString());
    }

    public static TxResponse delContactWay(String configId, Meta meta) {
        if (StrUtils.isEmpty(configId)) {
            return TxResponse.error("未找到需删除的联系我配置");
        }
        Map<String, String> map = new HashMap<>();
        map.put("config_id", configId);
        return TxRequest.getInstance(meta.getCorpid(), meta.getAppSecret())
                .post("/cgi-bin/externalcontact/del_contact_way", JSONUtil.toJsonStr(map));
    }

    private static QyContactWay turnQyContactWay(ContactWayInfo contactWay) {
        QyContactWay way = new QyContactWay();
        way.setType(contactWay.getType());
        way.setScene(contactWay.getScene());
        way.setRemark(contactWay.getRemark());
        way.setUser(contactWay.getUsers().stream().map(ContactVo::getId).collect(Collectors.toList()));
        way.setState(contactWay.getState());
        way.setSkipVerify(contactWay.isSkipVerify());
        way.setConfigId(contactWay.getConfigId());
        return way;
    }

    /**
     * 查看员工是否有权限
     *
     * @param users 是否存在权限的员工Map key = userId , value = userName
     * @return 返回错误信息
     */
    public static String allowExternalOfUsers(Map<String, String> users, Meta meta) {
        TxResponse txResponse = followUserList(meta);
        if (!txResponse.isSuccess()) {
            return txResponse.getError();
        }
        JSONArray followUsers = txResponse.getJson().getJSONArray("follow_user");
        for (Map.Entry<String, String> user : users.entrySet()) {
            if (!followUsers.contains(user.getKey())) {
                return "员工「" + user.getValue() + "」没有员工活码权限";
            }
        }
        return null;
    }
}
